package hanjia;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 * Author:    Dane
 * CreateTime:2025/2/14-下午10:33
 * Since  :   1.0
 * Version:   1.0
 */
public class LeetCode93 {

    public List<String> restoreIpAddresses(String s) {
        ArrayList<String> result = new ArrayList<>();
        if (s.length() > 12) {
            return result;
        }

        int start = 0;
        int pointNum = 0;

        backtracking(s, start, pointNum, result);
        return result;
    }

    private void backtracking(String s, int start, int pointNum, ArrayList<String> result) {

        if (pointNum==3){

            if (isValid(s,start,s.length()-1)){
                result.add(s);
            }
            return;
        }


        for (int i=start;i<s.length();i++){
            if (isValid(s,start,i)){
                s=s.substring(0,i+1)+'.'+s.substring(i+1);
                pointNum++;
                backtracking(s,i+2,pointNum,result);
                pointNum--;
                s=s.substring(0,i+1)+s.substring(i+2);
            }


        }

    }

    private boolean isValid(String s, int start, int i) {

        if (start>i){
            return false;
        }
        if (s.charAt(start)=='0'&&start!=i){
            return false;
        }
        int nums=0;

        for (int j=start;j<=i;j++){
            char c=s.charAt(j);
            if (c<'0'||c>'9'){
                return false;
            }
            nums=10*nums+c-'0';
            if (nums>255){
                return false;
            }

        }
        return true;





    }



}
